/**
 * @file loopbus_hal.h
 * @author Morris (morris@mnblog.cn)
 * @brief
 * @version 0.0.1
 * @date 2022-01-17
 *
 * @copyright Copyright (c) 2022
 *
 */
#ifndef __LOOPBUS_HAL_H__
#define __LOOPBUS_HAL_H__

#include <stdint.h>
#include "bsp.h"
#include "loopbus.h"

#define LOOPBUS_TIM_FREQ  (32768U) /* 总线时钟评论 */
#define LOOPBUS_BIT_TIME  (915U)   /* 总线bit时间, 915.51us */
#define LOOPBUS_ARR_COUNT ((uint16_t) ((LOOPBUS_BIT_TIME / (1.0 / LOOPBUS_TIM_FREQ * 1000000)) + 0.5f))

typedef int (*leds_set_pulse_callback_t)(uint16_t t1, uint16_t t2);
typedef void (*leds_set_ping_callback_t)(uint8_t t1);

/**
 * @brief 总线初始化
 *
 */
void loopbus_hal_init(void);

void loopbus_led_pulse_register(leds_set_pulse_callback_t callback);
void loopbus_led_ping_register(leds_set_ping_callback_t callback);

/**
 * @brief 主动开启总线的低功耗定时器
 *
 */
void loopbus_tim_start(void);

/**
 * @brief 主动通过回路上报设备(单元)状态
 *
 */
void loopbus_state_report(void);

/**
 * @brief 获取新命令事件
 *
 * @return loopbus_adapter_event_id_t
 */
loopbus_adapter_event_id_t loopbus_get_new_cmd_event(loopbus_adapter_event_msg_t *msg);

void loopbus_led_tick_update(void);

int loopbus_is_busy(void);

#endif /* __BSP_LOOPBUS_HAL_H__ */
